<!--
* @Author: bln
* @Date:  2023-11-09 14:12:03
 * @FilePath: \zmglpt-ui-mobile-v1.1\dev\pages\check\approve\creditVolationsApproval.vue
* @Description: 市管-售户信用违规行为维护审批--福建 统一待办审批跳转页
-->

<template>
	<view class="page">
		<!-- #ifndef H5-DINGDING -->
		<theme-navbar :is-white="false" :title="navTitle" :custom-back="goBack" style="z-index: 99999"></theme-navbar>
		<!-- #endif -->
		<view class="content">
			<u-form ref="uForm" v-model="baseInfo" label-position="top" label-width="auto">
				<u-collapse ref="cons" @change="changeHeight()">
					<u-collapse-item>
						<template #title>
							<u-row>
								<u-col span="12" style="display: flex">
									<view class="title"></view>
									<view class="basicHead">基础信息</view>
								</u-col>
							</u-row>
						</template>
						<u-form-item label="维护人">
							<u-input placeHolder="" type="text" :disabled="true" v-model="baseInfo.personName"></u-input>
						</u-form-item>
						<u-form-item label="维护时间">
							<u-input placeHolder="" type="text" :disabled="true" v-model="baseInfo.createTime"></u-input>
						</u-form-item>
						<u-form-item label="许可证号">
							<u-input placeHolder="" type="text" :disabled="true" v-model="baseInfo.licNo"></u-input>
						</u-form-item>
						<u-form-item label="负责人">
							<u-input placeHolder="" type="text" :disabled="true" v-model="baseInfo.managerName"></u-input>
						</u-form-item>
						<u-form-item label="企业（字号）名称">
							<u-input placeHolder="" type="text" :disabled="true" v-model="baseInfo.companyName"></u-input>
						</u-form-item>
						<u-form-item label="统一社会信用代码">
							<u-input placeHolder="" type="text" :disabled="true" v-model="baseInfo.businessLicNo"></u-input>
						</u-form-item>
						<u-form-item label="违法违规日期">
							<u-input placeHolder="" type="text" :disabled="true" v-model="baseInfo.manualDate"></u-input>
						</u-form-item>
					</u-collapse-item>
				</u-collapse>
				<u-gap :marginRight="getGapMarginRowNumber()" :marginLeft="getGapMarginRowNumber()"
					:height="$theme.cm_section_top_gap_height" :bg-color="$theme.cm_gap_color"></u-gap>
				<u-collapse ref="cons" @change="changeHeight()">
					<u-collapse-item>
						<template #title>
							<u-row>
								<u-col span="12" style="display: flex">
									<view class="title"></view>
									<view class="basicHead">违法违规类别选择</view>
								</u-col>
							</u-row>
						</template>
						<scroll-view :scroll-y="false" :scroll-x="true"
							:style="{ height: scrollRealHeight, marginTop: '5px', paddingBottom:'10px'}">
							<u-table class="tb-lay" ref="tbRef">
								<u-tr>
									<u-th width="4%">序号</u-th>
									<u-th width="12%">行为编码</u-th>
									<u-th width="28%">涉烟违法违规行为</u-th>
									<u-th width="10%">标准名称</u-th>
									<u-th width="13%">扣分值</u-th>
									<u-th width="23%">法律依据</u-th>
								</u-tr>
								<u-tr v-for="(it, index) in dataSource" :key="'ds' + index">
									<u-td width="4%">{{ index + 1 }}</u-td>
									<u-td width="12%">{{ it.targetCode || '&nbsp;' }}</u-td>
									<u-td width="28%">{{ it.targetName || '&nbsp;' }}</u-td>
									<u-td width="10%">{{ it.standardName || '&nbsp;' }}</u-td>
									<u-td width="13%">{{ it.creditScore || '&nbsp;' }}</u-td>
									<u-td width="23%">{{ it.lawBasis || '&nbsp;' }}</u-td>
								</u-tr>
							</u-table>
						</scroll-view>
					</u-collapse-item>
				</u-collapse>
				<u-form-item label="备注">
					<u-input placeHolder="" type="textarea" :maxlength="100" :disabled="true" v-model="baseInfo.remark"></u-input>
				</u-form-item>
				<!-- 承办部门意见 -->
				<!-- <u-gap :marginRight="getGapMarginRowNumber()" :marginLeft="getGapMarginRowNumber()"
					:height="$theme.cm_section_top_gap_height" :bg-color="$theme.cm_gap_color"></u-gap>
				<form-section title="承办部门意见"></form-section>
				<u-form-item label="">
					<u-radio-group v-model="departmentParams.approveMemoResult" @change="handleChangeRadio('departmentParams')" :disabled="departmentIsEdit">
						<u-radio v-for="(item,index) in departmentParamsList" :key="index" :label="item.label" :name="item.value">{{ item.label }}</u-radio>
					</u-radio-group>
				</u-form-item>
				<u-form-item label="">
					<u-input placeHolder="" type="textarea" :disabled="departmentIsEdit" v-model="departmentParams.approveMemo"></u-input>
				</u-form-item>
				<u-form-item label="签名">
					<u-input placeHolder="" type="text" :disabled="true" v-model="departmentParams.handlePerson"></u-input>
				</u-form-item>
				<u-form-item label="承办时间">
					<u-input placeHolder="" type="text" :disabled="true" v-model="departmentParams.applyDate"></u-input>
				</u-form-item> -->
				<!-- 负责人意见 -->
				<u-gap :marginRight="getGapMarginRowNumber()" :marginLeft="getGapMarginRowNumber()"
					:height="$theme.cm_section_top_gap_height" :bg-color="$theme.cm_gap_color"></u-gap>
				<form-section title="负责人意见"></form-section>
				<u-form-item label="">
					<u-radio-group v-model="responsibleParams.approveMemoResult" @change="handleChangeRadio('responsibleParams')" :disabled="responsibleIsEdit">
						<u-radio v-for="(item,index) in responsibleParamsList" :key="index" :label="item.label" :name="item.value">{{ item.label }}</u-radio>
					</u-radio-group>
				</u-form-item>
				<u-form-item label="">
					<u-input placeHolder="" type="textarea" :disabled="responsibleIsEdit" v-model="responsibleParams.approveMemo"></u-input>
				</u-form-item>
				<u-form-item label="签名">
					<u-input placeHolder="" type="text" :disabled="true" v-model="responsibleParams.handlePerson"></u-input>
				</u-form-item>
				<u-form-item label="承办时间">
					<u-input placeHolder="" type="text" :disabled="true" v-model="responsibleParams.applyDate"></u-input>
				</u-form-item>
				<!-- 审批历史开始 -->
				<u-gap :marginRight="getGapMarginRowNumber()" :marginLeft="getGapMarginRowNumber()"
					:height="$theme.cm_section_top_gap_height" :bg-color="$theme.cm_gap_color"></u-gap>
				<form-section title="审批历史"></form-section>
				<u-form-item label="" class="u-m-l-12">
					<flow-his-no-doc :bizUuid="prvParam.bizUuid" :processDefKey="prvParam.gfCode"
						:endTitle="endTitle"></flow-his-no-doc>
				</u-form-item>
				<!-- 审批历史结束 -->
			</u-form>
		</view>
		<view class="submit-btns btn-other-cls" v-if="ifSub">
			<u-button class="u-btn" type="primary" @click="handleRelease">提交</u-button>
		</view>
		<view class="submit-btns btn-other-cls" v-if="!approvalDisabled && !ifSub && !approveDis">
			<u-button class="u-btn" type="error" @click="handleApprovalChange('03','responsibleParams')">退回修改</u-button>
			<u-button class="u-btn" type="primary" @click="submit">审批</u-button>
		</view>
	</view>
</template>

<script>
	import DicChoice from '@/components/dic_choice.vue';
	import formSection from '@/components/form-section.vue';
	import FlowHisNoDoc from '@/components/flow-his-no-doc.vue';
	import creditService from '@/service/check/check.service.js'
	import { M_DAY_PLAN_STATUS } from '@/service/check/mapping/enum.js'
	
	export default {
		components: {
			formSection,
			FlowHisNoDoc,
			DicChoice
		},
		data() {
			return {
				step: '',
				endTitle: '',
				ifSub: false,
				approveDis: true,
				approvalDisabled: false,
				departmentIsEdit: true,
				responsibleIsEdit: true,
				prvParam: {}, // 路由参数
				baseInfo: {}, // 详情
				tableOriList: [], // 违法违规类别all
				scrollRealHeight: '50px',
				dictList: [{
					label: '依申请修复',
					value: '02'
				}, {
					label: '异议修复',
					value: '03'
				}, {
					label: '特殊修复',
					value: '04'
				}],
				// departmentParamsList: [{
				// 	label: '同意',
				// 	value: '01'
				// }, {
				// 	label: '不同意',
				// 	value: '02'
				// }],
				responsibleParamsList: [{
					label: '准予通过',
					value: '01'
				}, {
					label: '不予通过',
					value: '02'
				}],
				// departmentParams: {
				// 	approveMemoResult: '01',
				// 	approveMemo: '同意',
				// 	handlePerson: ' ',
				// 	applyDate: ' '
				// },
				responsibleParams: {
					approveMemoResult: '01',
					approveMemo: '准予通过',
					handlePerson: ' ',
					applyDate: ' '
				}
			}
		},
		onLoad () {
			this.prvParam = this.getRouteParam() || {}
			this.getVolationCategory()
			this.getBasicInfo()
		},
		computed: {
			// 违法违规类别checked
			dataSource () {
				const checkedKeys = this.baseInfo?.creditTargetHandList?.map(item => item.targetUuid) || []
				return this.tableOriList?.filter(item => checkedKeys.includes(item.targetUuid)) || []
			}
		},
		methods: {
			handleRelease() {
				this.toSelectPerson();
			},
			changeHeight() {
				this.$nextTick(() => {
					this.$refs.cons.init()
					// this.$refs.tbRef.init()
					setTimeout(() => {
					this.scrollRealHeight =
						this.$refs.tbRef.$el.clientHeight + 5 + "px";
					}, 100);
					
				})
			},
			getGapMarginRowNumber() {
				return '-' + this.$theme.cm_content_paddng_row.replace('rpx', '')
			},
			// 部门意见 / 负责人意见更新
			handleChangeRadio(type) {
				const result = this[type].approveMemoResult
				this[type].approveMemo = this[`${type}List`].find(item => item.value === result)?.label || ''
			},
			// 选择审批人
			toSelectPerson(docInsUuid = '') {
				this.$u.route({
					url: '/pages/commonPage/flowUserSelectComponent',
					params: {
						flowFormCode: this.prvParam.gfCode,
						docInsUuid: docInsUuid,
					},
				})
			},
			updateBackState(param) {
				if (param.flowUserSelectBack) {
					// 选择审批人
					let nextPersons = [];
					for (let fu of param.flowUserSelectBack) {
						nextPersons.push(fu.personId);
					}
					if (this.ifSub) {
						this.submitPerson(nextPersons.join(',')); // 提交选人
					} else {
						this.handleApprovalChange(this.responsibleParams.approveMemoResult, nextPersons.join(',')); // 审批通过
					}
				}
			},
			submitPerson(persons) {
				if (!persons) {
					this.$u.toast('请选择人员！');
					return;
				}
				let params = Object.assign({}, this.baseInfo)
				params.nextPersonUuids = persons;
				params.manualStatus = '01';
				this.$modal.showLoading('正在提交...');
				creditService
					.saveLicIllegalManageRecord(params)
					.then((res) => {
						this.$modal.hideLoading();
						if (res.success) {
							this.backPrev({
								paramKey: 'refresh',
								param: true
							});
						} else {
							this.$u.toast(res.msg);
						}
					})
					.catch((e) => {
						this.$modal.hideLoading();
						this.$u.toast(e.msg);
					});
			},
			// 获取违法违规类别
			async getVolationCategory () {
				try {
					this.$modal.showLoading('加载中...')
					const { data, success, msg } = await creditService.queryLicCreditHandTargetList({
						page: 1,
						limit: 15,
						manualRecordUuid: this.prvParam.bizUuid
					})
					if (success) {
						this.tableOriList = JSON.parse(JSON.stringify(data))
						setTimeout(() => {
					this.scrollRealHeight =
						this.$refs.tbRef.$el.clientHeight + 5 + "px";
					}, 100);
					} else {
						this.$modal.alert(msg, () => this.error())
					}
					this.$modal.hideLoading()
				} catch (e) {
					this.$modal.hideLoading()
					this.$modal.alert('加载失败' + e, () => this.error())
				}
			},
			// 获取信息详情
			async getBasicInfo () {
				try {
					this.$modal.showLoading('加载中...')
					const { bean, success, msg } = await creditService.getLicIllegalManagement({ manualRecordUuid: this.prvParam.bizUuid})
					if (success) {
						this.baseInfo = JSON.parse(JSON.stringify(bean))
						this.endTitle = bean.status == M_DAY_PLAN_STATUS.TERMINATION ? '结束' : '...'
						this.getCurrStatus()
						this.approvalInit()
					} else {
						this.$modal.alert(msg, () => this.error())
					}
					this.$modal.hideLoading()
				} catch (e) {
					this.$modal.hideLoading()
					this.$modal.alert('加载失败' + e, () => this.error())
				}
			},
			// 获取当前流程节点逻辑处理
			async getCurrStatus() {
				try {
					const { bean, success, msg } = await creditService.getFlowNodeStep({
						bizUuid: this.prvParam.bizUuid,
						processDefKey: this.prvParam.gfCode
					})
					if (success) {
						this.step = bean.step
						this.approveDis = false
						if (this.step == 'view') {
							this.approvalDisabled = true
						} else if (this.step == 'edit') {
							this.ifSub = true
						}
					} else {
						this.$modal.alert(msg, () => this.error())
					}
				} catch (e) {
					this.$modal.alert('加载失败' + e, () => this.error())
				}
			},
			// 审批初始化逻辑
			async approvalInit () {
				try {
					const { bean, success, msg } = await creditService.getApproves({
						bizUuid: this.prvParam.bizUuid,
						processDefKey: this.prvParam.gfCode
					})
					if (success) {
						// 承办部门意见
						// const departmentParams = bean['01']
						// this.departmentParams.approveMemoResult = departmentParams.approveResult || '01'
						// this.departmentParams.approveMemo = departmentParams.approveMemo || '同意'
						// this.departmentParams.handlePerson = departmentParams.approvePersonName || ''
						// this.departmentParams.applyDate = departmentParams.approveTime || ''
						// this.departmentIsEdit = !departmentParams.isCurryApproveNode
						// 负责人意见
						let responsibleParams = bean.end
						this.responsibleParams.approveMemoResult = responsibleParams.approveResult || '01'
						this.responsibleParams.approveMemo = responsibleParams.approveMemo || '准予通过'
						this.responsibleParams.handlePerson = responsibleParams.approvePersonName || ''
						this.responsibleParams.applyDate = responsibleParams.approveTime || ''
						this.responsibleIsEdit = !responsibleParams.isCurryApproveNode
					} else {
						this.$modal.alert(msg, () => this.error())
					}
				} catch (e) {
					this.$modal.alert('加载失败' + e, () => this.error())
				}
			},
			async submit() {
				try {
					this.$modal.showLoading('请稍后...')
					const { bean, success, msg } = await creditService.getFlowIsFinish({
						bizUuid: this.prvParam.bizUuid
					})
					if (success) {
						// if (!bean) {
						// 	// 提交承办部门意见
						// 	// this.handleApprovalChange('01', 'departmentParams')
						// 	// 是否流程最后一步
						// 	this.toSelectPerson(this.prvParam.bizUuid)
						// } else {
							// 提交负责人意见
							this.handleApprovalChange(this.responsibleParams.approveMemoResult, 'responsibleParams')
						// }
					} else {
						this.$u.toast(msg)
					}
					this.$modal.hideLoading()
				} catch (e) {
					this.$modal.hideLoading()
					this.$u.toast('请求失败' + e)
				}
			},
			// 退回处理 & 审批通过
			async handleApprovalChange(approveResult, type) {
				try {
					this.$modal.showLoading('请稍后...')
					const params = {
						menuCode: '',
						nextPersonUuids: '',
						approveResult: approveResult, // 退回
						bizUuid: this.prvParam.bizUuid,
						processDefKey: this.prvParam.gfCode,
						approveMemo: this[type].approveMemo,
						approveMemoResult: this[type].approveMemoResult, // 01 同意 02 不同意
					}
					const { success, msg } = await creditService.auditLicIllegalManagement(params)
					if (success) {
						this.backPrev({ paramKey: 'refresh', param: true })
					} else {
						this.$u.toast(msg)
					}
					this.$modal.hideLoading()
				} catch (e) {
					this.$modal.hideLoading()
					this.$u.toast('提交失败' + e)
				}
			}
		},
		error() {
			uni.navigateBack()
		}
	}
</script>

<style scoped lang="scss">
	.page {
		padding: 0;
	}
	.title {
		width: 8rpx;
		height: 32rpx;
		background: #0079f2;
		opacity: 1;
		margin-left: -5px;
		border-radius: 4rpx;
		float: left;
		margin-right: 16rpx;
	}
	.basicHead {
		height: 36rpx;
		line-height: 36rpx;
		text-align: center;
		float: left;
		font-weight: bold;
		font-size: 16px;
	}
	.sub-title {
		background: #f5f5f5;
		color: #989ea1;
		font-size: 24rpx;
		border-radius: 4px;
		margin-right: 10px;
		padding: 4px 8px;
	}
	.f-ctn {
		width: 100%;
	}
	.tb-lay {
		width: 900px;
		margin-left: 1px;
		margin-right: 1px;
	}
	.content {
		padding-left: $cm-content-paddng-row;
		padding-right: $cm-content-paddng-row;
		padding-top: $cm-conent-padding-column;
		padding-bottom: 70px;
	}
	.td-text {
		word-break: break-all;
		word-wrap: break-word;
	}
	/deep/.u-td {
		height: auto;
	}
</style>
